﻿/* 
 *  <copyright file="OrthoCamFollowActor.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;

using Microsoft.Xna.Framework;

using SobrietyEngine.Actor;
using SobrietyEngine.Components;
using SobrietyEngine.Guids;
using SobrietyEngine.Logging;

namespace SEGameExample.Components.Behavior
{
    class OrthoCamFollowActor:BaseComponent, IComponent
    {
        CGuid ActorGuid { get; set; }
        IActor actor;

        IPosition actorPos;
        IPosition myPos;

        IOrientation myRot;

        float bufferRadius;
        Vector3 offset = Vector3.Zero;

        float centerSpeed = 1.0f;

        public OrthoCamFollowActor(CGuid actor, float _bufferRadius, Vector3 _offset)
        {
            ActorGuid = actor;
            bufferRadius = _bufferRadius;
            offset = _offset;
        }

        public OrthoCamFollowActor(CGuid actor, float _bufferRadius)
        {
            ActorGuid = actor;
            bufferRadius = _bufferRadius;
        }

        public override Boolean InitComponent()
        {
            actor = (IActor)ActorGuid.Resolve();
            if (actor == null)            
                Logger.GetSingleton().WriteError("OrthoCamFollowActor::Init(): Cant find actor " + ActorGuid.Value);

            actorPos = actor.Get<IPosition>();
            myPos = Actor.Get<IPosition>();
            if(myPos == null || actorPos == null)
                Logger.GetSingleton().WriteError("OrthoCamFollowActor requires position components");

            //we need to face our player, so...
            myRot = Actor.Get<IOrientation>();

            SobrietyEngine.Components.Common.OrientationCmp rc = myRot as SobrietyEngine.Components.Common.OrientationCmp;
            Vector3 initalLookAt = myPos.PositionV3;
            initalLookAt.Z = -3;
            rc.SetLookAt(initalLookAt);

            return true;
        }

        Vector3 act;
        Vector3 me;
        Vector3 move;

        public override void Update(GameTime elapsedTime)
        {
            act = actorPos.PositionV3;
            act += offset;
            me = myPos.PositionV3;

            //ignore Z for ortho camera
            act.Z = 0;
            me.Z = 0;

            if (act - me == Vector3.Zero)
                return;

            //if we are out of range
            if (Vector3.DistanceSquared(act, me) > Math.Pow(bufferRadius, 2))
            {
                //total dist to actor
                float dist = Vector3.Distance(act, me);
                dist -= bufferRadius;

                move = act - me;
                move.Z = 0; //locked in otho, though might want to swivel the camera to look at the actor
                move.Normalize();

                move *= (dist + 0.01f);//add a bit to get past the buffer range, and snap back to actor

                myPos.PositionV3 += move;
            }
            else
            {
                //try to center on actor (or offcenter on actor, w/e)
                //total dist to actor
                float dist = Vector3.Distance(act, me);

                move = act - me;
                move.Z = 0; //locked in otho, though might want to swivel the camera to look at the actor
                move.Normalize();

                move *= dist * centerSpeed * elapsedTime.ElapsedGameTime.Milliseconds / 1000;

                myPos.PositionV3 += move;
            }
        }
    }
}
